package com.hrt.datasender.datastorage.strategy.monitordata;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.btcode.db.executer.IDataExecuter;
import com.btcode.db.result.Row;
import com.btcode.db.sqlparameter.SQLParameterList;
import com.btcode.orm.model.IModel;
import com.hrt.datasender.datadao.model.BcAccessParamModel;
import com.hrt.datasender.datadao.model.BcAccessTableModel;
import com.hrt.datasender.datadao.model.BcSendPlatformModel;
import com.hrt.datasender.datadao.model.BdS60RtModel;
import com.hrt.datasender.datadao.model.BdSourceModel;
import com.hrt.datasender.datadao.service.BcAccessParamService;
import com.hrt.datasender.datadao.service.BcAccessTableService;
import com.hrt.datasender.datadao.service.BdSourceSingleService;
import com.hrt.datasender.datastorage.StorageDataType;
import com.hrt.db.DBManager;

/**
 * 
 * <pre>
 * 获取ACCESS库实时数据策略
 * </pre>
 * 
 * @author FAN
 * @date 2018年9月12日下午5:25:00
 */
public class S60RTACCESS extends MonitorDataStrategy {
    private final int dataInterval = 60;
    
    @Override
    public void registerClearShedule() {

    }

    @Override
    public List<IModel> getStorageData(Calendar cal,List<String> parmList) {

        return null;
    }

    /**
     * 获取准备好要发送的数据时间
     * 
     * @return
     */
    @Override
    protected Long getReadySendDataTime(BcSendPlatformModel platform) {

        BcAccessTableModel models = BcAccessTableService.getInstance().getModel(platform);

        SQLParameterList pc = new SQLParameterList();

        Date startDate = getLastSendTime(platform);

        long firstDate = 0;

        String sql = "SELECT top 1 * FROM " + models.getRttable() + " WHERE " + models.getDatatime()
                + " > @startDate ORDER BY " + models.getDatatime() + " ASC";
        pc.put("startDate", startDate);
        IDataExecuter accessExecuter = DBManager.getAccessExecuter();
        List<Row> list = accessExecuter.queryList(sql, pc);

        if (list.size() < 1) {
            return null;
        }

        Date datetime = list.get(0).getDate("DATATIME");

        firstDate = datetime.getTime();

        return firstDate;
    }

    @Override
    public List<Row> getData(BcSendPlatformModel sendPlatForm, Long readySendDatatime) {

        BcAccessTableModel models = BcAccessTableService.getInstance().getModel(sendPlatForm);

        List<BcAccessParamModel> paramModels = BcAccessParamService.getInstance().getList();

        SQLParameterList pc = new SQLParameterList();

        Date datetime = new Date(readySendDatatime);

        List<Row> result = null;
        List<Row> dataResult = new ArrayList<>();

        String sql = "SELECT * FROM " + models.getRttable() + " WHERE " + models.getDatatime()
                + " >= @datetime ORDER BY " + models.getDatatime() + " ASC LIMIT 0,1";

        pc.put("datetime", datetime);

        result = DBManager.getAccessExecuter().queryList(sql, pc);

        for (Row row : result) {

            for (int j = 0; j < paramModels.size(); j++) {
                String paramMeas = row.getSafeString(paramModels.get(j).getAccessParam() + "_" + models.getMeas());
                double paramMeasD = 0;
                if (!"".equals(paramMeas)) {
                    paramMeasD = Double.parseDouble(paramMeas);
                }

                //DATATIME
                Date dateTime = row.getDate(models.getDatatime());
                long time = dateTime.getTime();

                String paramMeasSigin = row
                        .getSafeString(paramModels.get(j).getAccessParam() + "_" + models.getMeassigin());
                String datatime = row.getSafeString(models.getDatatime());

                Row item = new Row();
                item.put("MEAS_RE", paramMeasD);
                item.put("DATA_ID", paramModels.get(j).getParamCodeBus() + datatime);
                item.put("PARAM_CODE_BUS", paramModels.get(j).getParamCodeBus());
                item.put("CREATETIME", new Date());
                item.put("DATATIME", time);
                item.put("FLAG", "N");
//                if ("".equals(paramMeasSigin)) {
//                    item.put("FLAG", "N");
//                }
//                else {
//                    item.put("FLAG", paramMeasSigin);
//                }
                dataResult.add(item);
            }
        }

        return dataResult;
    }

    /**
     * 从原始数据表中获取数据
     * 
     * @return
     */
    public List<BdSourceModel> getSourceData() {

        List<BdSourceModel> sourceData = BdSourceSingleService.getInstance().getLastSourceData();

        return sourceData;
    }

    /**
     * 进行折算值处理
     * 
     * @return
     */
    public List<BdS60RtModel> processZS(List<BdSourceModel> sourceDatas) {

        return null;
    }

    /**
     * 进行普通处理
     * 
     * @return
     */
    public List<BdS60RtModel> processNormal(List<BdSourceModel> sourceDatas) {

        return null;
    }

    @Override
    public String getStoageTable() {
        return "BD_S60_RT";
    }

    @Override
    public String getNote() {
        return "如果是需要计算折算值站点,需配置conf.properties文件汇总的iscoefficient为true,coefficient值根据站点配置系数,"
                + "oxygencontent含氧量编码配置成该站点一样";
    }

    @Override
    public String getShowName() {
        return "ACCESS实时数据";
    }

    @Override
    public StorageDataType getDataType() {
        return StorageDataType.SECOND;
    }

    @Override
    public int getDataInterval() {
        // TODO Auto-generated method stub
        return dataInterval;
    }

    @Override
    public String getConsStr() {
        return "0 0/1 * * * ?";
    }

}
